package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.StuRequestParam;
import com.itheima.pojo.Student;
import com.itheima.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StuController {

    @Autowired
    private StuService stuService;

    /**
     * 条件分页查询学生
     * */
    @GetMapping
    public Result page(StuRequestParam stuRequestParam){
        //日志记录
        log.info("分页查询学生，{}", stuRequestParam);

        //调用业务层方法分页查询学生信息
        PageResult<Student> pageResult = stuService.page(stuRequestParam);

        //将业务层返回结果封装成Result对象返回
        return Result.success(pageResult);
    }

    /**
     * 批量删除学员
     * */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        //日志输出
        log.info("删除学员，{}", ids);

        //调用业务层方法，删除指定id的学员
        stuService.deleteByIds(ids);

        //返回成功结果
        return Result.success();
    }

    /**
     * 添加学员
     * */
    @PostMapping
    public Result add(@RequestBody Student student){
        //日志输出
        log.info("添加学员，{}", student);

        //调用业务层方法，添加学员
        stuService.add(student);

        //返回成功结果
        return Result.success();
    }

    /**
     * 根据id查找学员
     * */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        //日志输出
        log.info("根据id查找学员，{}", id);

        //调用业务层方法，根据id查找学员
        Student student = stuService.selectById(id);

        //返回结果
        return Result.success(student);
    }

    /**
     * 修改学员信息
     * */
    @PutMapping
    public Result update(@RequestBody Student student){
        //日志输出
        log.info("修改学员信息，{}", student);

        //调用业务层方法，修改学员信息
        stuService.update(student);

        //返回成功结果
        return Result.success();
    }

    /**
     * 处理学生违纪信息
     * */
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id, @PathVariable Integer score){
        //日志输出
        log.info("处理学生违纪信息，id={}, score={}", id, score);

        //调用业务层方法，处理学生违纪信息
        stuService.updateViolation(id, score);

        //返回成功结果
        return Result.success();
    }

}
